<template>
  <el-dialog :title="!dataForm.roleId ? '新增' : '修改'" :visible.sync="visible" width="60%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="110px">
      <el-form-item label="值班日期" prop="time">
        <el-date-picker v-model="dataForm.time" type="date" value-format="yyyy-MM-dd" clearable placeholder="选择日期"
          style="width: 100%">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="早班" prop="zaoBan">
        <el-select v-model="dataForm.zaoBan" multiple clearable placeholder="请选择" style="width: 100%;">
          <el-option v-for="item in careOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="午班" prop="zhongBan">
        <el-select v-model="dataForm.zhongBan" multiple clearable placeholder="请选择" style="width: 100%;">
          <el-option v-for="item in careOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="晚班" prop="wanBan">
        <el-select v-model="dataForm.wanBan" multiple clearable placeholder="请选择" style="width: 100%;">
          <el-option v-for="item in careOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { getDataInfo, savaDataInfo } from '@/api/hospital/working';
  export default {
    props: {
      careOptions: {
        type: Array,
        default: () => []
      }
    },
    data() {
      return {
        visible: false,
        dataForm: {},
        dataRule: {
          time: { required: true, message: '请输入', trigger: 'blur' },
          zaoBan: { required: true, message: '请输入', trigger: 'blur' },
          zhongBan: { required: true, message: '请输入', trigger: 'blur' },
          wanBan: { required: true, message: '请输入', trigger: 'blur' },
        },
      };
    },
    watch: {
      careOptions: {
        deep: true,
        immediate: true,
        handler() { }
      }
    },
    mounted() {
    },
    methods: {
      init(id) {
        this.visible = true;
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields();
          this.dataForm = { id, };
          if (id) {
            getDataInfo(id)
              .then((data) => {
                if (data && data.code === 0) {
                  this.dataForm = {
                    time: data.data.time,
                    zaoBan: data.data.zaoBan.split(','),
                    zhongBan: data.data.zhongBan.split(','),
                    wanBan: data.data.wanBan.split(','),
                  };
                }
              })
              .catch((err) => {
                console.log(err);
              });
          }
        });
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            const data = {
              time: this.dataForm.time,
              zaoBan: this.dataForm.zaoBan.join(','),
              zhongBan: this.dataForm.zhongBan.join(','),
              wanBan: this.dataForm.wanBan.join(','),
            }
            savaDataInfo(data)
              .then((res) => {
                if (res && res.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false;
                      this.$emit('refreshDataList');
                    },
                  });
                }
              })
              .catch((err) => {
                console.log(err);
              });
          }
        });
      },
    },
  };
</script>
